
/****** Object:  Stored Procedure [dbo].[sBaoCaoDoanhThuCuaSanPham] Script Date: 6/12/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[BaoCaoCongNoCuaKhachHang]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[BaoCaoCongNoCuaKhachHang]
GO
/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		04/07/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[BaoCaoCongNoCuaKhachHang]
(
	@MaKhachHang nvarchar(6),
	@TuNgay datetime,
	@DenNgay datetime
)

WITH ENCRYPTION
AS

	if(len(@MaKhachHang)=0) set @MaKhachHang = null
	
	select distinct 
		kh.*, (kh.SoLuong * kh.DonGia) as ThanhTien, ISNULL(tt.ThanhToan,0) AS ThanhToan	
	from v_KhachHangMuaHang as kh 
	left outer join v_KhachHangThanhToan as tt
	on kh.mactpx = tt.MaCTPX 
	where ((@MaKhachHang IS NULL) OR kh.makh = @MaKhachHang)
	AND ((@TuNgay is null) or (convert(date,@TuNgay)<=convert(date,NgayXuatKho)))
	AND ((@DenNgay is null) or (convert(date,@DenNgay)>=convert(date,NgayXuatKho)))
		
	Select * FROM tbl_Congty
go

--GRANT EXECUTE ON [dbo].[spPhieuXuatGetList] TO [sa]
--GO
